Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List components from other sources #4742

Merged
merged 14 commits into from
Jun 15, 2021

Conversation

valaparthvi
Copy link
Member

@valaparthvi valaparthvi commented May 24, 2021

What type of PR is this?
/kind feature
/kind code-refactoring

What does this PR do / why we need it:
This PR adds the support to see components created/managed by tools other than odo.

Which issue(s) this PR fixes:

Fixes #4688

PR acceptance criteria:

  • Unit test

  • Integration test

  • Documentation

  • Update changelog

  • I have read the test guidelines

How to test changes / Special notes to the reviewer:
Manually create a deployment and deploymentconfig in openshift web console or any other tool. Make sure it is labelled with the following labels -

        app: httpd
        app.kubernetes.io/instance: example
        app.kubernetes.io/managed-by: console
        app.kubernetes.io/managed-by-version: v4.8.0
        app.kubernetes.io/name: example
        app.kubernetes.io/part-of: httpd

Or better yet, it follows these labels - https://github.com/redhat-developer/app-labels

With OCP4, it will be easy to create a component of a deployment kind by using the samples provided by ocp, but to create a deploymentConfig kind component you can use the following yaml -

apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: example
  namespace: myproject
  labels:
    app: httpd
    app.kubernetes.io/instance: example
    app.kubernetes.io/managed-by: console
    app.kubernetes.io/managed-by-version: v4.8.0
    app.kubernetes.io/name: example
    app.kubernetes.io/part-of: httpd
spec:
  selector:
    app: httpd
  replicas: 3
  template:
    metadata:
      labels:
        app: httpd
        app.kubernetes.io/instance: example
        app.kubernetes.io/managed-by: console
        app.kubernetes.io/managed-by-version: v4.8.0
        app.kubernetes.io/name: example
        app.kubernetes.io/part-of: httpd
    spec:
      containers:
        - name: httpd
          image: >-
            image-registry.openshift-image-registry.svc:5000/openshift/httpd:latest
          ports:
            - containerPort: 8080

Also create a few components with the help of odo.

Run odo list and you should be able to see the non-odo components in the output that looks something like this -

➜  odo git:(listcom) ✗ odo list --all-apps
W0524 13:12:51.685576   13526 warnings.go:70] extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
W0524 13:12:55.884893   13526 warnings.go:70] extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
W0524 13:12:58.278964   13526 warnings.go:70] extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
Devfile Components: 
APP      NAME      PROJECT       TYPE      STATE
app1     njs2i     myproject     njs2i     Pushed

Other Components running on the cluster(read-only): 
APP            NAME              PROJECT       TYPE
sample-app     nodejs-sample     myproject     nodejs-sample
httpd          example           myproject     example

Also, do test with odo list --app sample-app, odo list --all-apps -o json, odo list.

Note - This is work-in-progress and you might find some commented code and incomplete tests, I am yet to add unit and integrations tests as well, but I am waiting on it to make sure that everyone agrees on the implmentation.

@openshift-ci openshift-ci bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. Required by Prow. kind/feature Categorizes issue as a feature request. For PRs, that means that the PR is the implementation kind/code-refactoring labels May 24, 2021
@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. label May 24, 2021
@valaparthvi valaparthvi force-pushed the listcom branch 3 times, most recently from d720aa1 to 8b668b5 Compare May 24, 2021 09:03
Comment on lines 927 to 929
if err != nil {
return ComponentList{}, errors.Wrap(err, "Unable to get component")
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question here. Why do we return if err!=nil even though there are some other checks pending in the function, for e.g. this check - https://github.com/openshift/odo/pull/4742/files#diff-a84453ec52dba47b60234fd3aba56849e52e2bae2577c082dcf1a79ef6b25855R946 ?

if client != nil {

Why do we avoid that and return? Shouldn't we check for if err == nil { continue } and execute rest of the code? I have seen this type of logic in almost all places. For some it makes sense, but for others I think we should continue executing the program and if it does not make sense to do so, then perhaps we should add a comment about why we are returning without executing the rest of the code.

Other examples are -

  1. pkg/odo/cli/component/list.go:264 - https://github.com/openshift/odo/pull/4742/files#diff-a5ee7fb0c76cd592580dce46cda117d8a483452979b0da40d644906f88cba343R264
  2. pkg/odo/cli/component/list.go:274 - https://github.com/openshift/odo/pull/4742/files#diff-a5ee7fb0c76cd592580dce46cda117d8a483452979b0da40d644906f88cba343R274
  3. ..and many more

Context("when components are not created/managed by odo", func() {
var appName, compName string
JustBeforeEach(func() {
//CreateNonOdoComponent
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can someone help me with this one? I plan on creating a deployment and deployment-config manually via API, but I need to prepare a lot of data before doing that. Do we already have helper functions that might help here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just create deployment.yaml with the most simple deployment that you can come up with. Add the same labels to it as WebConsole does and just call kubectl apply -f deplyomet.yaml

@openshift-ci openshift-ci bot removed the approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. label Jun 7, 2021
@valaparthvi valaparthvi changed the title WIP: List components from other sources List components from other sources Jun 9, 2021
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. Required by Prow. label Jun 9, 2021
@@ -1651,13 +1642,16 @@ func GetMachineReadableFormatForList(s2iComps []Component) ComponentList {
}

// GetMachineReadableFormatForCombinedCompList returns list of devfile and s2i components in machine readable format
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe update this comment?

// To pass operands such as !=, append a ! prefix to the value.
// For E.g. map[string]string{"app.kubernetes.io/managed-by": "!odo"}
// Using != operators also means that resource will be filtered even if it doesn't have the key.
// So a resource not labelled with key "app.kubernetes.io/managed-by" will also be returned.
func ConvertLabelsToSelector(labels map[string]string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not extend the labels here to be a list of structs here like

struct {
Key string
Value string
Operand Operand
}

type Operand string

var Equal Operand = "="
var NotEqual Operand = "!="

might be more future proof?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of this label map

labels map[string]string
struct {
Key string
Value string
Operand Operand
}

type Operand string

var Equal Operand = "="
var NotEqual Operand = "!="

So the function would look like this ConvertLabelsToSelector(labels []Label).

So I can do something like this

labels := []Label{
   {
   Key: "app.kubernetes.io/managed-by",
   Operand: NotEqual,
    Value: "odo"
     }
}

ConvertLabelsToSelector(labels)

The conversion of []Label to string would also become simplified

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would allow for room for extension and also less mistakes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me as if this will require a significant amount of effort and it seems a little out of scope for this PR. Is it alright if I take this in another PR?

Also, can you give me an example of possible mistakes that could happen here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you change strings.Replace(v, "!", "", -1) to strings.Replace(v, "!", "", 1) wouldn't it the problem of doing !!odo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kadel I am not sure I understand your problem question correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am good from my end, please address @kadel's question and then remove the hold

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized something. ! can't be present in a label or its value.
I think that we are ok with this. front.

But I think that I'm really missing here is a unit test for this function :-( It became much more complicated than it used to be, and it definetly deserves unit test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kadel Unit tests! That can be easily fixed! I'll get to it.

@girishramnani I looked at how k8s.io/apimachinery does this label selector thing and it is a little complicated, they have special interface and methods for it. See here - https://github.com/openshift/odo/blob/89ac67e425bffe8c400645bca933d687b5ac8b19/vendor/k8s.io/apimachinery/pkg/fields/selector.go#L430

I will rely on Unit tests for now, and hopefully we will not run into corner cases, or catch them if we do.

@kadel
Copy link
Member

kadel commented Jun 10, 2021

tested, it it looks good
/approve

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. label Jun 10, 2021
@valaparthvi
Copy link
Member Author

/test v4.7-integration-e2e

The failing test passes locally. Retrying.

@girishramnani
Copy link
Contributor

/hold
Just want to make sure that someone doesn't lgtm until all comments are resolved 🙂

@openshift-ci openshift-ci bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. Required by Prow. label Jun 11, 2021
@valaparthvi
Copy link
Member Author

/retest
Unit test windows failure.

@valaparthvi
Copy link
Member Author

/unhold
Addressed concern in #4742 (comment)

@openshift-ci openshift-ci bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. Required by Prow. label Jun 14, 2021
@valaparthvi
Copy link
Member Author

/override ci/prow/psi-unit-test-mac
/override ci/prow/psi-unit-test-windows

Reason - These are optional checks.

@openshift-ci
Copy link

openshift-ci bot commented Jun 14, 2021

@valaparthvi: Overrode contexts on behalf of valaparthvi: ci/prow/psi-unit-test-mac, ci/prow/psi-unit-test-windows

In response to this:

/override ci/prow/psi-unit-test-mac
/override ci/prow/psi-unit-test-windows

Reason - These are optional checks.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@girishramnani
Copy link
Contributor

/approve
/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. Required by Prow. label Jun 14, 2021
@openshift-ci
Copy link

openshift-ci bot commented Jun 14, 2021

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: girishramnani, kadel

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [girishramnani,kadel]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@@ -2684,3 +2684,48 @@ func TestGetGitOriginPath(t *testing.T) {
})
}
}

func TestConvertLabelsToSelector(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

1 similar comment
@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. area/refactoring Issues or PRs related to code refactoring kind/feature Categorizes issue as a feature request. For PRs, that means that the PR is the implementation lgtm Indicates that a PR is ready to be merged. Required by Prow.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

List components created by sources other than odo
7 participants