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

[Antctl] Add an output format to better display multi-line string responses #3426

Closed
dreamtalen opened this issue Mar 9, 2022 · 11 comments · Fixed by #3589
Closed

[Antctl] Add an output format to better display multi-line string responses #3426

dreamtalen opened this issue Mar 9, 2022 · 11 comments · Fixed by #3589
Assignees
Labels
area/component/antctl Issues or PRs releated to the command line interface component good first issue Good for newcomers kind/feature Categorizes issue or PR as related to a new feature. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.

Comments

@dreamtalen
Copy link
Contributor

dreamtalen commented Mar 9, 2022

Describe the problem/challenge you have

When working on implementing policy recommendation CLI through Antctl, I found current output formatters (table, json, and yaml) of Antctl are not good at displaying multi-line string responses.
For example, I added a command antctl get-policy-reco-result and its response is a multi-line string containing network policies in yaml format.
Currently, the output of this command in the yaml formatter will look like this:

>>> antctl get-policy-reco-result -o yaml
 |
    apiVersion: crd.antrea.io/v1alpha1
    kind: ClusterNetworkPolicy
    metadata:
      name: recommend-allow-acnp-kube-system-rlebf
    spec:
      appliedTo:
      - namespaceSelector:
          matchLabels:
            kubernetes.io/metadata.name: kube-system
      egress:
      - action: Allow
        to:
        - podSelector: {}
      ingress:
      - action: Allow
        from:
        - podSelector: {}
      priority: 5
      tier: Platform

The output using json formatter will look like this:

>>> antctl get-policy-reco-result -o json
"apiVersion: crd.antrea.io/v1alpha1\nkind: ClusterNetworkPolicy\nmetadata:\n  name: recommend-allow-acnp-kube-system-rlebf\nspec:\n  appliedTo:\n  - namespaceSelector:\n      matchLabels:\n        kubernetes.io/metadata.name: kube-system\n  egress:\n  - action: Allow\n    to:\n    - podSelector: {}\n  ingress:\n  - action: Allow\n    from:\n    - podSelector: {}\n  priority: 5\n  tier: Platform\n---\napiVersion: crd.antrea.io/v1alpha1\nkind: ClusterNetworkPolicy\nmetadata:\n  name: recommend-allow-acnp-flow-aggregator-ops0e\nspec:\n  appliedTo:\n  - namespaceSelector:\n      matchLabels:\n        kubernetes.io/metadata.name: flow-aggregator\n  egress:\n  - action: Allow\n    to:\n    - podSelector: {}\n  ingress:\n  - action: Allow\n    from:\n    - podSelector: {}\n  priority: 5\n  tier: Platform\n"

What I'm expecting output is similar to fmt.Print(responseString) directly which let user could redirect the output into a yaml file (antctl get-policy-reco-result > network_policies.yml) and apply it afterward:

>>> antctl get-policy-reco-result -o rawString
apiVersion: crd.antrea.io/v1alpha1
kind: ClusterNetworkPolicy
metadata:
  name: recommend-allow-acnp-kube-system-rlebf
spec:
  appliedTo:
  - namespaceSelector:
      matchLabels:
        kubernetes.io/metadata.name: kube-system
  egress:
  - action: Allow
    to:
    - podSelector: {}
  ingress:
  - action: Allow
    from:
    - podSelector: {}
  priority: 5
  tier: Platform

Describe the solution you'd like

Add a new output formatter called "plain" or "rawString" in https://github.com/antrea-io/antrea/blob/main/pkg/antctl/command_definition.go#L42.

Anything else you would like to add?

@dreamtalen dreamtalen added kind/feature Categorizes issue or PR as related to a new feature. area/component/antctl Issues or PRs releated to the command line interface component labels Mar 9, 2022
@luolanzone luolanzone added the good first issue Good for newcomers label Mar 11, 2022
@dreamtalen
Copy link
Contributor Author

Hi @antoninbas @tnqn, may I have your opinions regarding this issue?

@Atish-iaf
Copy link
Contributor

Can i take this issue?

@dreamtalen
Copy link
Contributor Author

Can i take this issue?

Sure, thank you Kumar.

@Atish-iaf
Copy link
Contributor

Hi @dreamtalen , can u please help me to reproduce this issue as antctl get-policy-reco-result is not working and using antctl get networkpolicy -o yaml and antctl get networkpolicy -o json doesn't give output as described in the issue. Thanks.

@dreamtalen
Copy link
Contributor Author

Hi @dreamtalen , can u please help me to reproduce this issue as antctl get-policy-reco-result is not working and using antctl get networkpolicy -o yaml and antctl get networkpolicy -o json doesn't give output as described in the issue. Thanks.

Hi @Atish-iaf, antctl get-policy-reco-result is a command I'm still developing right now so you won't be able to run it. To reproduce this issue, You could change the result to a multiple-line string of any current available antctl commands, for example antctl log-level.

@Atish-iaf
Copy link
Contributor

Hi @dreamtalen , i got following responses on running antctl commands:

root@kind-worker2:/# antctl get networkpolicy -o yaml
- appliedToGroups:
  - cg-pods-a-then-c
  metadata:
    creationTimestamp: null
    generation: 1
    name: 13e3a092-4c9c-451e-824b-4425ea289c32
    uid: 13e3a092-4c9c-451e-824b-4425ea289c32
  priority: 1
  rules:
  - action: Drop
    direction: Out
    enableLogging: false
    from: {}
    name: egress-drop-a023c9e
    services:
    - port: 80
      protocol: TCP
    to:
      addressGroups:
      - ca7fa6a1-a194-58eb-bb66-124af5c2e4cf
  sourceRef:
    name: acnp-deny-cg-with-a-to-z-egress
    type: AntreaClusterNetworkPolicy
    uid: 13e3a092-4c9c-451e-824b-4425ea289c32
  tierPriority: 250
- appliedToGroups:
  - 79422d97-26a2-5ff8-aac0-721a7b767d20
  metadata:
    creationTimestamp: null
    generation: 1
    name: c6db9b34-faf1-4876-9e61-3bfb0b806fbb
    uid: c6db9b34-faf1-4876-9e61-3bfb0b806fbb
  rules:
  - action: Allow
    direction: In
    enableLogging: false
    from:
      addressGroups:
      - 4eac2aa0-b85b-5b4d-bb78-1b516014600b
    priority: -1
    services:
    - port: 80
      protocol: TCP
    to: {}
  sourceRef:
    name: test-network-policy
    namespace: default
    type: K8sNetworkPolicy
    uid: c6db9b34-faf1-4876-9e61-3bfb0b806fbb
root@kind-worker2:/# antctl get networkpolicy -o json
[
  {
    "metadata": {
      "name": "13e3a092-4c9c-451e-824b-4425ea289c32",
      "uid": "13e3a092-4c9c-451e-824b-4425ea289c32",
      "generation": 1,
      "creationTimestamp": null
    },
    "rules": [
      {
        "direction": "Out",
        "from": {},
        "to": {
          "addressGroups": [
            "ca7fa6a1-a194-58eb-bb66-124af5c2e4cf"
          ]
        },
        "services": [
          {
            "protocol": "TCP",
            "port": 80
          }
        ],
        "action": "Drop",
        "enableLogging": false,
        "name": "egress-drop-a023c9e"
      }
    ],
    "appliedToGroups": [
      "cg-pods-a-then-c"
    ],
    "priority": 1,
    "tierPriority": 250,
    "sourceRef": {
      "type": "AntreaClusterNetworkPolicy",
      "name": "acnp-deny-cg-with-a-to-z-egress",
      "uid": "13e3a092-4c9c-451e-824b-4425ea289c32"
    }
  },
  {
    "metadata": {
      "name": "c6db9b34-faf1-4876-9e61-3bfb0b806fbb",
      "uid": "c6db9b34-faf1-4876-9e61-3bfb0b806fbb",
      "generation": 1,
      "creationTimestamp": null
    },
    "rules": [
      {
        "direction": "In",
        "from": {
          "addressGroups": [
            "4eac2aa0-b85b-5b4d-bb78-1b516014600b"
          ]
        },
        "to": {},
        "services": [
          {
            "protocol": "TCP",
            "port": 80
          }
        ],
        "priority": -1,
        "action": "Allow",
        "enableLogging": false
      }
    ],
    "appliedToGroups": [
      "79422d97-26a2-5ff8-aac0-721a7b767d20"
    ],
    "sourceRef": {
      "type": "K8sNetworkPolicy",
      "namespace": "default",
      "name": "test-network-policy",
      "uid": "c6db9b34-faf1-4876-9e61-3bfb0b806fbb"
    }
  }
]

Still unable to reproduce the issue, can you please provide more clarification? Thanks.

@dreamtalen
Copy link
Contributor Author

dreamtalen commented Mar 30, 2022

Hi @Atish-iaf, please read my last comment. The output of the command antctl get networkpolicy you ran is not a multiple-line string, and you should change the output to a multiple-line string of an antctl command or add a new antctl command for testing.

@Atish-iaf
Copy link
Contributor

Hi @dreamtalen , may i know actual transformedResponse used in the new command so that i could get multiple-line string output ? And also the exact output of the api for the new command if possible. Thanks.

@dreamtalen
Copy link
Contributor Author

Hi @Atish-iaf
The transformedResponse I used when defining the new antctl command in pkg/antctl/antctl.go is transformedResponse: reflect.TypeOf("").
My new command doesn't have a fixed output, you could create some general multiple-line string with \n. For example, "Hello World\nHow are you doing today\nHope all is well with your go\nAnd code"

Atish-iaf added a commit to Atish-iaf/antrea that referenced this issue Apr 6, 2022
…ponses.

Current output formatters (table, json, and yaml) of Antctl are not good at displaying multi-line string responses.
Added a new output formatter called "rawString" whose output is similar to fmt.Print(responseString) which directly lets user redirect the output into a file and apply it afterwards.

Fixes antrea-io#3426

Signed-off-by: Kumar Atish <atish.iaf@gmail.com>
Atish-iaf added a commit to Atish-iaf/antrea that referenced this issue Apr 6, 2022
…ponses.

Current output formatters (table, json, and yaml) of Antctl are not good at displaying multi-line string responses.
Added a new output formatter called "raw" whose output is similar to fmt.Print(responseString) which directly lets user redirect the output into a file and apply it afterwards.

Fixes antrea-io#3426

Signed-off-by: Kumar Atish <atish.iaf@gmail.com>
dreamtalen pushed a commit to dreamtalen/antrea that referenced this issue May 10, 2022
…ponses.

Current output formatters (table, json, and yaml) of Antctl are not good at displaying multi-line string responses.
Added a new output formatter called "raw" whose output is similar to fmt.Print(responseString) which directly lets user redirect the output into a file and apply it afterwards.

Fixes antrea-io#3426

Signed-off-by: Kumar Atish <atish.iaf@gmail.com>
@github-actions
Copy link
Contributor

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment, or this will be closed in 90 days

@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jun 30, 2022
@dreamtalen
Copy link
Contributor Author

Close this issue since CLI of policy recommendation has been moved to Theia.

Atish-iaf added a commit to Atish-iaf/antrea that referenced this issue Jul 6, 2022
Current output formatters (table, json, and yaml) of Antctl are
not good at displaying multi-line string responses.

Add a new output formatter `raw` whose output is similar to
fmt.Print(responseString) to better display multi-line string responses.

Fixes antrea-io#3426

Signed-off-by: Kumar Atish <atish.iaf@gmail.com>
jianjuns pushed a commit that referenced this issue Jul 6, 2022
Current output formatters (table, json, and yaml) of antctl are not
good for displaying multi-line string responses.

Add a new output formatter `raw` whose output is similar to
fmt.Print(responseString) to better display multi-line string
responses.

Fixes #3426

Signed-off-by: Kumar Atish <atish.iaf@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/component/antctl Issues or PRs releated to the command line interface component good first issue Good for newcomers kind/feature Categorizes issue or PR as related to a new feature. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants